package Interfaces;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Auxiliary.Sequence_Type;
import DataView.Expr;
import DataView.StateObserverInfo;
import Store.StateObserver;

public class SOI_manageElement 
{
	JPanel container;
	ShowStateObserversAndInvariants showStateObs;
	CampoTesto nome = new CampoTesto();
	JPanel centerPanel;
	boolean modifica;
	StateObserver originalElement;
	JComboBox typeSelector;
	Sequence_Type avableTypes;
	AreaTesto expression = new AreaTesto();
	
	public JPanel show(ShowStateObserversAndInvariants so, boolean modifica, StateObserver original, Sequence_Type availableTypes) 
	{
		showStateObs = so;
		this.modifica=modifica;
		originalElement=original;
		avableTypes=availableTypes;
		
		container = new JPanel();
		container.setLayout(new BorderLayout());
		
		String  str="";
		if(modifica)
			str=originalElement.getName();
		container.add(getIntestazione(str),BorderLayout.NORTH);		
		container.add(getCenterArea(),BorderLayout.CENTER);
		container.add(getButtonArea(), BorderLayout.SOUTH);
		
		return container;
	}

	private JPanel getIntestazione(String namestr) 
	{
		JPanel pannelloIntestazione = new JPanel();
		pannelloIntestazione.setLayout(new BorderLayout());
		JLabel label = new JLabel("New StateObserver");
		if(modifica) label.setText("Modify StateObserver");
		label.setFont(new Font("Tahoma", Font.BOLD, 18));
		pannelloIntestazione.add(label, BorderLayout.NORTH);
		if(modifica) 
		{
			nome.crea("Inserisci qui il nome");
			nome.setText(namestr);
			pannelloIntestazione.add(nome.get(), BorderLayout.CENTER);
		}
		else pannelloIntestazione.add(nome.crea("Insert Name"), BorderLayout.CENTER);
		pannelloIntestazione.add(new JLabel(), BorderLayout.SOUTH);
		
		return pannelloIntestazione;
	}

	private JPanel getCenterArea() 
	{
		centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout());
	
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new GridLayout(1,4));
		northPanel.add(new JLabel("Select Type"));
		typeSelector=new JComboBox();
		int max = 0;
		if(avableTypes!=null) max=avableTypes.size();
		for (int i = 0 ; i<max ; i++)
			typeSelector.addItem(avableTypes.get(i).getName());
		if(modifica) typeSelector.setSelectedItem(originalElement.getType().getName());
		northPanel.add(typeSelector);
		centerPanel.add(northPanel, BorderLayout.NORTH);
		
		expression.crea("Insert Invariant", 1);
		if(modifica) expression.setText(originalElement.getExpr().getDescription());
		centerPanel.add(expression.get(), BorderLayout.CENTER);
		
		return centerPanel;
	}

	private JPanel getButtonArea() 
	{
		JPanel buttonPanel = new JPanel();
		JButton ok = new JButton("Save");
		JButton cancel = new JButton("Cancel");
		buttonPanel.setLayout(new GridLayout(1,2));
		buttonPanel.add(ok);
		buttonPanel.add(cancel);
		
		ok.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				StateObserverInfo info = new StateObserverInfo();
				info.setInfo(nome.getText(), avableTypes.get(typeSelector.getSelectedIndex()), new Expr(expression.getText())) ;
				info.getType().values();
				if(info.getName().length()==0)
				{
					showStateObs.showMessage("Insert the name for StateObserver");
				}
				else {showStateObs.container.removeAll();
				if(modifica) showStateObs.updateStateObserverInv.updStateObserver(originalElement, info);
				else showStateObs.updateStateObserverInv.addStateObserver(info);
				}
			}
		});
		cancel.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent e) 
			{
				showStateObs.container.removeAll();
				showStateObs.showSOI();
			}
		});
		
		return buttonPanel;
	}
	
	public void refresh()
	{
		centerPanel.removeAll();
		centerPanel.add(getCenterArea(), BorderLayout.CENTER);
		centerPanel.revalidate();
	}

}

